package com.fancymusic.mongoService;


import com.fancymusic.mongoEntity.MongoMusic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class MongoMusicService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存对象
     *
     * @param mongoMusic
     * @return
     */
    public String saveObj(MongoMusic mongoMusic) {
        mongoMusic.setCreateTime(new Date());
        mongoMusic.setUpdateTime(new Date());
        mongoTemplate.save(mongoMusic);
        return "添加成功";
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<MongoMusic> findAll() {
        List<MongoMusic> musicList = mongoTemplate.findAll(MongoMusic.class);

        musicList.sort(Comparator.comparing(MongoMusic::getDownloadQuantity));
        return musicList;
    }

    /***
     * 根据id查询
     * @param id
     * @return
     */
    public MongoMusic getMongoMusicById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query, MongoMusic.class);
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return
     */
    public MongoMusic getMongoMusicByName(String name) {
        Query query = new Query(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query, MongoMusic.class);
    }

    /**
     * 更新对象
     *
     * @param mongoMusic
     * @return
     */
    public String updateMongoMusic(MongoMusic mongoMusic) {
//        Query query = new Query(Criteria.where("_id").is(MongoMusic.getId()));
//        Update update = new Update().set("publish", MongoMusic.getPublish()).set("info", MongoMusic.getInfo()).set("updateTime",
//                new Date());
        // updateFirst 更新查询返回结果集的第一条
//        mongoTemplate.updateFirst(query, update, MongoMusic.class);
        // updateMulti 更新查询返回结果集的全部
        // mongoTemplate.updateMulti(query,update,Book.class);
        // upsert 更新对象不存在则去添加
        // mongoTemplate.upsert(query,update,Book.class);
        return "success";
    }

    /**
     * 模糊查询
     *
     * @param search
     * @return
     */
    public List<MongoMusic> findMongoMusicLikes(String search) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        //criteria.where("name").regex(search);
        Pattern pattern = Pattern.compile("^.*" + search + ".*$", Pattern.CASE_INSENSITIVE);
        criteria.where("name").regex(pattern);
        List<MongoMusic> lists = mongoTemplate.findAllAndRemove(query, MongoMusic.class);
        return lists;
    }


}
